package blog.gui;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;

import blog.ConstString;
import blog.SysRuntime;
import blog.util.StringUtils;

public class AdvanceBodyEdit extends TextBox implements IBaseGui, CommandListener
{
	private Command cmd_ok = new Command( ConstString.CMD_OK, Command.OK, 0);
	private Command cmd_insertShortCut = new Command( ConstString.CMD_INSERT_SHORT_CUT, Command.SCREEN, 1);
	private Command cmd_insertHtmlLink = new Command( ConstString.CMD_INSERT_HTML_LINK, Command.SCREEN, 2);
	private Command cmd_insertOnlineImage = new Command( ConstString.CMD_INSERT_ONLINE_IMAGE, Command.SCREEN, 3);
	private Command cmd_insertHtmlTag = new Command( ConstString.CMD_INSERT_HTML_TAG, Command.SCREEN, 4);
	private Command cmd_back = new Command( ConstString.CMD_CANCEL, Command.BACK, 1);

	private static AdvanceBodyEdit _instance;
	
	public static AdvanceBodyEdit getInstance()
	{
		if ( _instance==null )
		{
			_instance = new AdvanceBodyEdit( "", 4096, TextField.ANY );
		}
		return _instance;
	}
	
	private AdvanceBodyEdit( String text, int maxSize, int constraints )
	{
		super(ConstString.GUI_TITLE_TEXT_EDIT, text, maxSize, constraints );
		
		addCommand( this.cmd_ok );
		this.addCommand( cmd_insertShortCut );
		this.addCommand( cmd_insertHtmlLink );
		this.addCommand( cmd_insertOnlineImage );
		this.addCommand( cmd_insertHtmlTag );
		this.addCommand( cmd_back );
		this.setCommandListener( this );
	}

	public int getIdentify()
	{
		return 0;
	}

	public void notifyChanged(Object param)
	{

	}

	public void commandAction(Command cmd, Displayable arg1)
	{
		if ( cmd==this.cmd_insertShortCut )
		{
			ShortCutListGui nextScreen = ShortCutListGui.getInstance();
			nextScreen.reloadData();
			nextScreen.setInsertPos( getCaretPosition() );
			SysRuntime.guiManager.show(nextScreen);
		}
		else if ( cmd==cmd_insertHtmlLink )
		{
			HtmlInsertLinkGui nextScreen = new HtmlInsertLinkGui( getCaretPosition() );
			SysRuntime.guiManager.show(nextScreen);
		}
		else if ( cmd==cmd_insertOnlineImage )
		{
			HtmlInsertOnlineImageGui nextScreen = new HtmlInsertOnlineImageGui( getCaretPosition() );
			SysRuntime.guiManager.show(nextScreen);
		}
		else if ( cmd==cmd_insertHtmlTag )
		{
			HtmlTagPromptGui nextScreen = new HtmlTagPromptGui(getCaretPosition(), 0, false);
			SysRuntime.guiManager.show(nextScreen);
		}
		else if ( cmd==cmd_back )
		{
			SysRuntime.guiManager.back( );
		}
		else if ( cmd==cmd_ok )
		{
			BlogEntryGui.getInstance( ).m_body.setString( this.getString( ) );
			SysRuntime.guiManager.back( );
		}
	}
	
	public void insertSomething( String str, int insertPos )
	{
		if ( str!=null )
		{
			insert( str, insertPos );
		}
	}

}